//
//  IssueMateViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/9/29.
//

#import "IssueMateViewController.h"
#import "IssueTypeView.h"
#import "MateTypeViewController.h"
#import <BRPickerView/BRPickerView.h>
#import "MateMapViewController.h"
#import "AboutPopView.h"
#import "IssueMateViewController.h"
#import "PeopleNumberPopView.h"
@interface IssueMateViewController ()
@property (nonatomic , strong) HeaderView  * IssueBtn;
@property (nonatomic , strong) IssueTypeView * bgView;
@property (nonatomic , strong) IssueTypeView * bgView1;
@property (nonatomic , strong) IssueTypeView * bgView3;
@property (nonatomic , strong) IssueTypeView * bgView5;
@property (nonatomic , strong) IssueTypeView * bgView4;
@property (nonatomic , strong) IssueTypeView * bgView6;

@property (nonatomic , strong) NSMutableDictionary * seletedDic;

@end

@implementation IssueMateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"发布活动");
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(TopHeight+200);
    }];
    
    [self.view addSubview:self.bgView4];
    [self.bgView4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH);
            make.height.mas_equalTo(54);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(10);
    }];
    
    [self.view addSubview:self.bgView6];
    [self.bgView6 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH);
            make.height.mas_equalTo(54);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(70);
    }];
    [self.view addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH);
            make.height.mas_equalTo(54);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(130);
    }];
  
    [self.view addSubview:self.bgView1];
    [self.bgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH);
            make.height.mas_equalTo(54);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(190);
    }];
   
    [self.view addSubview:self.bgView3];
    [self.bgView3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH);
            make.height.mas_equalTo(54);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(250);
    }];
 
    [self.view addSubview:self.bgView5];
    [self.bgView5 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH);
            make.height.mas_equalTo(54);
            make.top.equalTo(self.zx_navBar.mas_bottom).offset(310);
    }];
   
    [self.view addSubview:self.IssueBtn];
    [self.IssueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(SCREEN_WIDETH- 32);
            make.height.mas_equalTo(54);
            make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-20);
    }];

}
-(void)SQ_buttonclicktype{
    [self.view endEditing: YES];
    MateTypeViewController * vc = [[MateTypeViewController alloc]init];
    if (self.bgView.rightName) {
        vc.typeString = self.bgView.rightName;
    }  
    [self SQ_pushViewControllerFull:vc];
    WS(weakSelf);
    [vc setTypeStringBlock:^(NSString * _Nonnull typeString) {
        weakSelf.bgView.rightName = typeString;
        [weakSelf SQ_fabuButtoncolor];
    }];
}
-(void)SQ_peopleNumber{
    PeopleNumberPopView* vc = [[PeopleNumberPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 360+FootHeight)];
    vc.isAll = self.bgView1.isAll;
    vc.totalField.text = self.bgView1.numbernan;
    vc.nanField.text = self.bgView1.numbernan;
    vc.nvField.text = self.bgView1.numbernv;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
      pop.hemStyle = LSTHemStyleBottom;
    pop.avoidKeyboardSpace = -10;
    [pop pop];
    WS(weakSelf);
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [weakSelf.view endEditing:YES];
    }];
    __weak PeopleNumberPopView * view = vc;
    [vc.addButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
        weakSelf.bgView1.isAll = view.isAll;
        weakSelf.bgView1.numbernan = view.totalField.text;
        if (view.isAll) {
            if ([view.totalField.text isNotEmpty]) {
                weakSelf.bgView1.rightName = [NSString stringWithFormat:@"%@:%@",SQNSLocalString(@"总人数"),view.totalField.text];
            }
        } else {
            weakSelf.bgView1.numbernan = view.nanField.text;
            weakSelf.bgView1.numbernv = view.nvField.text;
            if ([view.nanField.text isNotEmpty]&&[view.nvField.text isNotEmpty]) {
                weakSelf.bgView1.rightName =[NSString stringWithFormat:@"男性:%@ 女性:%@",view.nanField.text,view.nvField.text];
            }else if([view.nanField.text isNotEmpty]&&![view.nvField.text isNotEmpty]){
                weakSelf.bgView1.rightName =[NSString stringWithFormat:@"男性:%@",view.nanField.text];
            }else if(![view.nanField.text isNotEmpty]&&[view.nvField.text isNotEmpty]){
                weakSelf.bgView1.rightName =[NSString stringWithFormat:@"女性:%@",view.nvField.text];
            }
        }
    }];
}
 
-(void)SQ_StartTime{
    [self.view endEditing: YES];
    WS(weakSelf);
    BRStringPickerView *textPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    textPickerView.pickerStyle = [self SQ_getStyle];
    textPickerView.title = @"活动时间类型选择";
    // 设置数据源
    textPickerView.dataSourceArr = @[@"长期", @"法定节假日",@"周末", @"工作日",@"自选日期"];
    textPickerView.selectIndex = [textPickerView.dataSourceArr indexOfObject:self.bgView3.rightLabel.text];
    [textPickerView setResultModelBlock:^(BRResultModel * _Nullable resultModel) {
        weakSelf.bgView3.numbernan = [NSString stringWithFormat:@"%ld",(long)resultModel.index];
        if ([resultModel.value isEqualToString:@"自选日期"]) {
            [weakSelf SQ_zhixuanTime];
        } else {
            weakSelf.bgView3.rightName = resultModel.value;
            [weakSelf SQ_fabuButtoncolor];
        }
      
    }];
    [textPickerView show];
}

-(void)SQ_zhixuanTime{
    WS(weakSelf);
    BRDatePickerView * pickView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHM];
    pickView.pickerStyle = [self SQ_getStyle];
   
    pickView.minDate = [NSDate date];
    [pickView setResultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        weakSelf.bgView3.rightName = selectValue;
        [weakSelf SQ_fabuButtoncolor];
    }];
    [pickView show];
}


-(void)SQ_addressClick{
    WS(weakSelf);
    MateMapViewController * vc = [[MateMapViewController alloc]init];
    [weakSelf.navigationController pushViewController:vc animated:YES];
    [vc setAddressBlock:^(NSMutableDictionary * _Nonnull dic, NSString * _Nonnull address) {
        weakSelf.bgView5.rightName = address;
        weakSelf.seletedDic = dic;
        [weakSelf SQ_fabuButtoncolor];
    }];
  
}

//内容
-(void)SQ_popAboutPopView{
    AboutPopView* vc = [[AboutPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 320+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    pop.avoidKeyboardSpace = -10;
    [pop pop];
    vc.fsTextView.text = self.bgView6.rightName;
    WS(weakSelf);
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [weakSelf.view endEditing:YES];
    }];
    [vc.hideButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    __weak AboutPopView * vcs = vc;
    [vc.addButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if ([vcs.fsTextView.formatText isNotEmpty]) {
            [weakSelf.view endEditing:YES];
            weakSelf.bgView6.rightName = vcs.fsTextView.formatText;
            [wk_pop dismiss];
            [weakSelf SQ_fabuButtoncolor];
        }
    }];
}

//标题
-(void)SQ_popAboutPopView4{
    AboutPopView* vc = [[AboutPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 320+FootHeight)];
    vc.label.text = SQNSLocalString(@"活动标题");
    vc.fsTextView.maxLength = 15;
    vc.fsTextView.text = self.bgView4.rightName;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    pop.avoidKeyboardSpace = -10;
    [pop pop];
    if ([self.bgView6.rightName isNotEmpty]) {
        vc.fsTextView.text = self.bgView6.rightName;
    }
    WS(weakSelf);
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [weakSelf.view endEditing:YES];
    }];
    [vc.hideButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    __weak AboutPopView * vcs = vc;
    [vc.addButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if ([vcs.fsTextView.formatText isNotEmpty]) {
            [weakSelf.view endEditing:YES];
            weakSelf.bgView4.rightName = vcs.fsTextView.formatText;
            [wk_pop dismiss];
            [weakSelf SQ_fabuButtoncolor];
        }
    }];
}

-(void)SQ_fabuButtoncolor{
    BOOL isColor = YES;
    if (![self.bgView.rightName isNotEmpty]) {
        isColor = NO; //类型必填
    }
    if (![self.bgView1.rightName isNotEmpty]) {
        isColor = NO;//人数
    }
    if (![self.bgView3.rightName isNotEmpty]) {
        isColor = NO;//开始时间
    }
    if (![self.bgView4.rightName isNotEmpty]) {
        isColor = NO;//开始时间
    }
    if (![self.bgView5.rightName isNotEmpty]) {
        isColor = NO;//开始时间
    }
    if (![self.bgView6.rightName isNotEmpty]) {
        isColor = NO;//描述内容
    }
    if (isColor) {
        _IssueBtn.backgroundColor = SQColor3DA59D;
        _IssueBtn.colorString = @"#FFFFFF";
    } else {
        _IssueBtn.backgroundColor = SQHexColor(@"#3DA59D", 0.4);
        _IssueBtn.colorString = @"#999999";
    }
}
-(void)SQ_addBtonClick{
    if (![self.bgView.rightName isNotEmpty]) {
        return;
    }
    if (![self.bgView1.rightName isNotEmpty]) {
        return;
    }
    if (![self.bgView3.rightName isNotEmpty]) {
        return;
    }
    if (![self.bgView4.rightName isNotEmpty]) {
        return;
    }
    if (![self.bgView5.rightName isNotEmpty]) {
        return;
    }
    if (![self.bgView6.rightName isNotEmpty]) {
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.bgView.rightName forKey:@"category"];//类型
    [dic setObject:self.bgView6.rightName forKey:@"content"];//内容
    [dic setObject:self.bgView4.rightName forKey:@"title"];//标题
    [dic setObject:self.bgView5.rightName forKey:@"address"];//地址
   
    [dic setObject:self.bgView3.numbernan forKey:@"timeType"];//时间
    [dic setObject:self.bgView3.rightName forKey:@"time"];//时间
    [dic setObject:[self.seletedDic objectForKey:@"lat"]  forKey:@"lat"];
    [dic setObject:[self.seletedDic objectForKey:@"lng"] forKey:@"lng"];
    //人数
    if (self.bgView1.isAll) {
        [dic setObject:@"0" forKey:@"userType"];
        [dic setObject:self.bgView1.numbernan forKey:@"userCount"];
    } else {
        [dic setObject:@"1" forKey:@"userType"];
        if (![self.bgView1.numbernv isNotEmpty]) {
            self.bgView1.numbernv = @"0";
        }
        if (![self.bgView1.numbernan isNotEmpty]) {
            self.bgView1.numbernan = @"0";
        }
        [dic setObject:self.bgView1.numbernan forKey:@"manCount"];
        [dic setObject:self.bgView1.numbernv forKey:@"womanCount"];
    }
    [HttpTool postJson:@"lug/publish" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"发布成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing: YES];
}

- (HeaderView *)IssueBtn{
    if (!_IssueBtn) {
        _IssueBtn = [[HeaderView alloc]init];
        _IssueBtn.backgroundColor = SQHexColor(@"#3DA59D", 0.4);
        _IssueBtn.text = SQNSLocalString(@"发布");
        _IssueBtn.colorString = @"#999999";
        _IssueBtn.font = FONT_MEDIUM(14);
        _IssueBtn.layer.cornerRadius = 27;
        _IssueBtn.btn.mm_acceptEventInterval = 3;
        _IssueBtn.layer.masksToBounds = YES;
        [_IssueBtn.btn addTarget:self action:@selector(SQ_addBtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _IssueBtn;
}
- (IssueTypeView *)bgView{
    if (!_bgView) {
        _bgView = [[IssueTypeView alloc]init];
        _bgView.name = SQNSLocalString(@"类型");
        _bgView.rightLabel.text = SQNSLocalString(@"点击选择");
        [_bgView.btn addTarget:self action:@selector(SQ_buttonclicktype) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView;
}
- (IssueTypeView *)bgView1{
    if (!_bgView1) {
        _bgView1 = [[IssueTypeView alloc]init];
        _bgView1.name = SQNSLocalString(@"人数");
        _bgView1.rightLabel.text = SQNSLocalString(@"点击选择");
        _bgView1.isAll = YES;
        _bgView1.numbernan = @"";
        _bgView1.numbernv = @"";
        [_bgView1.btn addTarget:self action:@selector(SQ_peopleNumber) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView1;
}

- (IssueTypeView *)bgView3{
    if (!_bgView3) {
        _bgView3 = [[IssueTypeView alloc]init];
        _bgView3.name = SQNSLocalString(@"时间");
        _bgView3.rightLabel.text = SQNSLocalString(@"点击选择");
        [_bgView3.btn addTarget:self action:@selector(SQ_StartTime) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _bgView3;
}
 
- (IssueTypeView *)bgView5{
    if (!_bgView5) {
        _bgView5 = [[IssueTypeView alloc]init];
        _bgView5.name = SQNSLocalString(@"地址");
        _bgView5.rightLabel.text = SQNSLocalString(@"点击输入");
        [_bgView5.btn addTarget:self action:@selector(SQ_addressClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView5;
}
- (IssueTypeView *)bgView4{
    if (!_bgView4) {
        _bgView4 = [[IssueTypeView alloc]init];
        _bgView4.name = SQNSLocalString(@"标题");
        _bgView4.rightLabel.text = SQNSLocalString(@"点击添加标题");
        [_bgView4.btn addTarget:self action:@selector(SQ_popAboutPopView4) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView4;
}
- (IssueTypeView *)bgView6{
    if (!_bgView6) {
        _bgView6 = [[IssueTypeView alloc]init];
        _bgView6.name = SQNSLocalString(@"描述");
        _bgView6.rightLabel.text = SQNSLocalString(@"点击添加描述");
        [_bgView6.btn addTarget:self action:@selector(SQ_popAboutPopView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bgView6;
}
-(BRPickerStyle*)SQ_getStyle{
    BRPickerStyle * style = [BRPickerStyle new];
    style.alertViewColor = UIColor.whiteColor;
    style.pickerColor = UIColor.whiteColor;
    style.titleBarColor= UIColor.whiteColor;
    style.hiddenTitleLine = YES;
    style.titleTextColor = SQColor333333;
    style.cancelTextColor = SQColor333333;
    style.doneTextColor = SQColor333333;
    style.rowHeight = 50;
    style.pickerTextColor = SQColor333333;
    style.separatorColor = SQColor3DA59D;
    return style;
}

@end
